package com.ayou.eggguard.farm.controller;

import com.ayou.common.utils.PageBean;
import com.ayou.common.utils.Result;
import com.ayou.eggguard.farm.pojo.RelatedDrug;
import com.ayou.eggguard.farm.pojo.TreatMethod;
import com.ayou.eggguard.farm.pojo.TreatmentPlan;
import com.ayou.eggguard.farm.service.TreatMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/treatMethod")
public class TreatMethodController {
    @Autowired
    private TreatMethodService treatMethodService;

    @GetMapping("/getMethods")
    public Result<PageBean<TreatMethod>> getMethods(int pageNum,
                                                    int pageSize,
                                                    @RequestParam(required = false) String methodName){
        PageBean<TreatMethod> pb= treatMethodService.getMethods(pageNum,pageSize,methodName);
        return Result.success(pb);
    }

    @GetMapping("/getMethodsList")
    public Result<PageBean<TreatmentPlan>> getMethodsList(int pageNum,
                                                          int pageSize,
                                                          @RequestParam(required = false) String methodName){
        PageBean<TreatmentPlan> pb=treatMethodService.getMethodsList(pageNum,pageSize,methodName);
        return Result.success(pb);
    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        treatMethodService.delete(id);
        return Result.success();
    }

    @GetMapping("/deleteMethod")
    public Result deleteMethod(String methodName){
        treatMethodService.deleteMethod(methodName);
        return Result.success();
    }

    @PostMapping("/addTreatmentPlan")
    public Result addTreatmentPlan(@RequestParam String methodName,
                                   @RequestParam String methodDescription,
                                   @RequestBody List<RelatedDrug> drugList){
        treatMethodService.addTreatmentPlan(methodName,methodDescription,drugList);
        return Result.success();
    }

    @PostMapping("/updateTreatmentPlan")
    public Result updateTreatmentPlan(@RequestParam String oldName,
                                      @RequestParam String newName,
                                      @RequestParam String methodDescription,
                                      @RequestBody List<RelatedDrug> drugList){
        treatMethodService.deleteMethod(oldName);
        treatMethodService.addTreatmentPlan(newName,methodDescription,drugList);
        return Result.success();
    }

    @GetMapping("/getMethodName")
    public Result<List<String>> getMethodName(){
        List<String> list= treatMethodService.getMethodName();
        return Result.success(list);
    }
}
